package cn.sc.allinpay.allfavour.business.common.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import java.math.BigDecimal;
import java.util.List;

import cn.pai.common.adapter.BaseRecyAdapter;
import cn.pai.common.utils.AmtUtils;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.business.common.data.CheckCouponData;
import cn.sc.allinpay.allfavour.business.common.dialog.CheckInputAmtDialog;

/**
 * description：验券卡券适配器，支持多张券添加备注
 * author：pany
 * on 2018/7/17 15:02
 */
public class CheckCouponOldAdapter extends BaseRecyAdapter<CheckCouponOldAdapter.MyViewHoder, CheckCouponData> {

    private OnAdapterListener onAdapterListener;
    private List<String> remarks;

    public CheckCouponOldAdapter(List<String> remarks) {
        super();
        this.remarks = remarks;
    }

    public CheckCouponOldAdapter(List<CheckCouponData> datas, List<String> remarks) {
        super(datas);
        this.remarks = remarks;
    }

    @Override
    public int layoutId() {
        return R.layout.item_check_coupon;
    }

    @Override
    public MyViewHoder onCreateViewHolder(View view) {
        return new MyViewHoder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHoder holder, int position, final CheckCouponData data) {
        holder.tvName.setText(data.getCouponName());
        if (0 == data.getType()) {//金额
            holder.llNumber.setVisibility(View.GONE);
            holder.llAmt.setVisibility(View.VISIBLE);
            holder.tvBalance.setText("剩余：" + AmtUtils.changeF2Y(data.getBalance()) + data.getUnit());
            holder.tvUseAmt.setText(AmtUtils.formatY(data.getUseAmt()));
            holder.tvUseAmt.setOnClickListener(new MyAmtClickListener(holder.itemView.getContext(), data));
            holder.tvDisUseAmt.setText(AmtUtils.formatNegY(data.getUseAmt()));
        } else if (1 == data.getType()) {//抵扣
            holder.llNumber.setVisibility(View.VISIBLE);
            holder.llAmt.setVisibility(View.GONE);
            holder.tvBalance.setText("剩余：" + data.getBalance() + data.getUnit());
            holder.tvNumber.setText(String.valueOf(data.getUseNumber()));
            MyCountClickListener listener = new MyCountClickListener(data);
            holder.btnReduce.setOnClickListener(listener);
            holder.btnAddit.setOnClickListener(listener);
            BigDecimal discountAmt = new BigDecimal(data.getDiscountedAmt()).multiply(new BigDecimal(data.getUseNumber()));
            holder.tvDisUseAmt.setText(AmtUtils.changeNegF2Y(discountAmt));
        } else if (2 == data.getType()) {//计次
            holder.llNumber.setVisibility(View.VISIBLE);
            holder.llAmt.setVisibility(View.GONE);
            holder.tvBalance.setText("剩余：" + data.getBalance() + data.getUnit());
            holder.tvNumber.setText(String.valueOf(data.getUseNumber()));
            MyCountClickListener listener = new MyCountClickListener(data);
            holder.btnReduce.setOnClickListener(listener);
            holder.btnAddit.setOnClickListener(listener);
            BigDecimal discountAmt = new BigDecimal(data.getDiscountedAmt()).multiply(new BigDecimal(data.getUseNumber()));
            holder.tvDisUseAmt.setText(AmtUtils.changeNegF2Y(discountAmt));
        }

        holder.spRemark.setAdapter(new CouponTypeAdapter(holder.itemView.getContext(), remarks));
        holder.spRemark.setSelection(TextUtils.isEmpty(data.getRemark()) ? 0 : remarks.indexOf(data.getRemark()));
        holder.spRemark.setOnItemSelectedListener(new RemarkItemSelect(data));
    }

    public void setOnAdapterListener(OnAdapterListener onAdapterListener) {
        this.onAdapterListener = onAdapterListener;
    }

    /**
     * 金额
     */
    private class MyAmtClickListener implements View.OnClickListener {

        private Context context;
        private CheckCouponData data;

        public MyAmtClickListener(Context context, CheckCouponData data) {
            this.context = context;
            this.data = data;
        }

        @Override
        public void onClick(View view) {
            CheckInputAmtDialog dialog = new CheckInputAmtDialog(context, data);
            dialog.setOnDialogListener(new CheckInputAmtDialog.OnDialogListener() {

                @Override
                protected void onConfirm(String amt) {
                    data.setUseAmt(amt);
                    data.setUse(!TextUtils.isEmpty(amt) ? true : false);
                    notifyDataSetChanged();
                    if (onAdapterListener != null) {
                        onAdapterListener.onDataChange();
                    }
                }

            });
            dialog.show();
        }
    }

    /**
     * 记次
     */
    private class MyCountClickListener implements View.OnClickListener {

        private CheckCouponData data;

        public MyCountClickListener(CheckCouponData data) {
            this.data = data;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btnReduce:
                    int curReduce = data.getUseNumber();// 当前照片张数
                    if (curReduce > 0) {
                        data.setUseNumber((curReduce - 1));
                    }
                    data.setUse(data.getUseNumber() > 0);//使用数量大于0时表示选中
                    notifyDataSetChanged();
                    break;
                case R.id.btnAddit:
                    int curAddit = data.getUseNumber();// 当前照片张数
                    int max = Integer.valueOf(data.getBalance());//当前剩余数量
                    if (curAddit < max) {
                        data.setUseNumber(curAddit + 1);
                    }
                    data.setUse(data.getUseNumber() > 0);//使用数量大于0时表示选中
                    notifyDataSetChanged();
                    break;
            }
            if (onAdapterListener != null) {
                onAdapterListener.onDataChange();
            }
        }
    }

    /**
     * 备注信息选择
     */
    private class RemarkItemSelect implements AdapterView.OnItemSelectedListener {

        private CheckCouponData data;

        public RemarkItemSelect(CheckCouponData data) {
            this.data = data;
        }

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if (i == 0) {
                data.setRemark("");
            } else {
                data.setRemark(remarks.get(i));
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }

    public interface OnAdapterListener {
        /**
         * 数据变化
         */
        void onDataChange();
    }

    class MyViewHoder extends RecyclerView.ViewHolder {

        AppCompatTextView tvName;//卡券名称
        AppCompatTextView tvBalance;//最大使用

        RelativeLayout llNumber;//数量选择界面
        AppCompatButton btnReduce;//减少
        AppCompatButton btnAddit;//增加
        AppCompatTextView tvNumber;//使用数量

        RelativeLayout llAmt;//金额输入界面
        AppCompatTextView tvUseAmt;//输入金额

        AppCompatSpinner spRemark;//备注

        AppCompatTextView tvDisUseAmt;//抵扣使用金额

        public MyViewHoder(View itemView) {
            super(itemView);
            // TODO Auto-generated constructor stub
            tvName = itemView.findViewById(R.id.tvName);
            tvBalance = itemView.findViewById(R.id.tvBalance);

            llNumber = itemView.findViewById(R.id.llNumber);
            btnReduce = itemView.findViewById(R.id.btnReduce);
            btnAddit = itemView.findViewById(R.id.btnAddit);
            tvNumber = itemView.findViewById(R.id.tvNumber);

            llAmt = itemView.findViewById(R.id.llAmt);
            tvUseAmt = itemView.findViewById(R.id.tvUseAmt);

            spRemark = itemView.findViewById(R.id.spRemark);

            tvDisUseAmt = itemView.findViewById(R.id.tvDisUseAmt);
        }
    }
}
